// pages/nutrition/nutrition.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    xiangqing:[],
    tupian:'',
    tilte:'',
    reliang:'',
    sercherStorage: [],
    StorageFlag: false,
    aijilu:{},
    componentData:[
      {
        color:'#7DE3EF',
        name:'蛋白质',
        g:0,
        percent:'35%',
      },
      {
        color:'#1E85FB',
        name:'脂肪',
        g:0,
        percent:'42%',
      },
      {
        color:'#AF8FFD',
        name:'碳水',
        g:0,
        percent:'20%'
      },
    ],
    recDishData:[{des:'做法步骤:第1步、苹果洗净切小块。第2步、加入食用油,适量白糖。第3步、小火翻炒,淋淀粉',star:3.5},1,1,1],
    recSPData:[],
  alikeFoods:[],
  tabooFoods:[],
  degreeData:['相对较低','相对一般','相对较高'],
  },
  getFullStar:function(e){

    console.log(e.currentTarget.dataset.i);
    return 3
  },
  jumpCaiPu:function(e){
    //e.currentTarget.dataset.text获取点击的模块的值
      console.log(e);
      wx.navigateTo({
        url: '../dishDetails/dishDetails?info='+JSON.stringify(e.currentTarget.dataset.index),
      })
      console.log(e.currentTarget.dataset.index);
      },
      getDetail(name){
        wx.showLoading({
          title: '正在加载中！',
        })
        wx.request({
          url: 'https://www.daizhongdai.xyz:3007/getDishDetail?name='+name,
          success:res=>{
            console.log(res.data.data.data[0]);
            var d= this.data.componentData
            d[0].g = Number(res.data.data.data[0]['蛋白质（克）']).toFixed(2)
            d[1].g = Number( res.data.data.data[0]['脂肪（克）']).toFixed(2)
            d[2].g =  Number(res.data.data.data[0]['碳水化物（克）']).toFixed(2)
            d[0].percent = this.Percentage( d[0].g,100)
            d[1].percent = this.Percentage( d[1].g,100)
            d[2].percent = this.Percentage( d[2].g,100)
            this.setData({
              detail:res.data.data.data[0],
              componentData:d

            })
          },complete:res=>{
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      },
       Percentage:function(number1) { 
        return number1*100/100+"%"
    },
    getGeuessYourLike:function(name){
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/like/query?name='+name,
        success:(res)=>{
          console.log(res.data.data.data);
          this.setData({
            caidan:res.data.data.data,
          }) 
        },fail:()=>{
       
        }
      })
      console.log(name);
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/getserch?name='+name,
        success:(res)=>{
          console.log(res.data.data[0]);
          this.setData({
            xiangqing:res.data.data[0],
            tilte:res.data.data[0].name,
            tupian:res.data.data[0].smallImgUrl,
            aijilu:res.data.data[0]
          })
          wx.request({
            url: 'https://www.daizhongdai.xyz:3007/insert/scann?avator='+this.data.tupian+'&'+'title='+this.data.tilte+'&'+'brief='+this.data.aijilu.measurement,
            method:'GET',
            header: {
              'content-type': 'application/json' ,// 默认值
              'sessiontoken':wx.getStorageSync('token')
            },
            success:(res)=>{
              console.log(res);
              console.log('插入记录成功');
            }
          })
      
        }
      })


    },

    getRecSP:function(){
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/index/tj',
        success:res=>{
          console.log(res.data.data.data);
          this.setData({
            recSPData:res.data.data.data
          })
        }
      })
    },
    goToDetail:function(e){
      console.log(e.currentTarget.dataset.i);
      var i = e.currentTarget.dataset.i
      var dd = this.data.recSPData
      var d = {
        title:dd[i].title,
        thumb:dd[i].thumb,
        fl:dd[i].fl,
        yl:dd[i].yl,
        steptext:dd[i].steptext,
      }
      wx.navigateTo({
        url: '../dishDetails/dishDetails?info='+JSON.stringify(d),
      })
    },
    aiThis:function(e){
      console.log(e.currentTarget.dataset.i);
      var i = e.currentTarget.dataset.i
      var d = this.data.alikeFoods
      this.onLoad({
      name:JSON.stringify([d[i].name])  
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    console.log(options.name);
    this.getRecSP()
    var n = JSON.parse(options.name)
    console.log(n+"6666");
   this. getGeuessYourLike(n[0].name || n)
 
    this.getDetail(n[0].name || n)
    if(!n[0].name)
    return
    for(var i = 1;i<n.length;i++){
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/getserch?name='+n[i].name,
        success:res=>{
          console.log(res.data.data);
          if(res.data.data.length!=0){
            var d = {
              name:res.data.data[0].name,
              img:res.data.data[0].smallImgUrl
            }
            var alikeFoods = this.data.alikeFoods
            alikeFoods.push(d)
            this.setData({
              alikeFoods
            })
          }
        }
      })
    }
 
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})